package com.yoomiito.app.widget.expand

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.animation.Animation
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.yoomiito.app.R
import com.yoomiito.app.model.sdd.SddRights
import com.yoomiito.app.model.sdd.SddRightsTip
import com.yoomiito.app.utils.DensityUtil
import com.yoomiito.app.utils.ImageLoader
import com.yoomiito.app.utils.L
import com.yoomiito.app.widget.dialog.SendMoreTipDialog


class ExpandChildView @JvmOverloads constructor(
        context: Context,
        attrs: AttributeSet? = null,
        defStyleAttr: Int = 0,
        defStyleRes: Int = 0
) : LinearLayout(context, attrs, defStyleAttr, defStyleRes) {
    private var mHeight = 0
    private var mShowHeight = 0
    var isExpand: Boolean = false
    var showViewSize = 0
    private val heights = mutableMapOf<View, Int>()

    init {
        orientation = VERTICAL
        val p = DensityUtil.dp2px(33f)
        setPadding(p, 0, p, 0)
    }

    fun setData(data: List<SddRights>) {
        data.forEachIndexed { index, item ->
            val view = LayoutInflater.from(context).inflate(R.layout.item_sc_rcy, null, false)
            view.tag = index
            heights[view] = 0
            view.findViewById<ImageView>(R.id.image).let {
                ImageLoader.getInstance().into(context, item.image, it)
            }
            view.findViewById<TextView>(R.id.tv_1).text = item.title
            view.findViewById<TextView>(R.id.tv_2).text = item.smallTitle
            view.findViewById<ImageView>(R.id.tip).let {
//                it.visibility = when{
//                    item.tips==null|| item.tips.isEmpty() ->View.GONE
//                    else->View.VISIBLE
//                }
                it.visibility = View.GONE
                it.setOnClickListener {
                    showTip1Dialog(item)
                }
            }
            val lp = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)
            addView(view, lp)
        }
        addOnLayoutChangeListener(object :OnLayoutChangeListener{
            override fun onLayoutChange(v: View?, left: Int, top: Int, right: Int, bottom: Int, oldLeft: Int, oldTop: Int, oldRight: Int, oldBottom: Int) {
                removeOnLayoutChangeListener(this)
                initHeight()
            }
        })
    }

    private fun showTip1Dialog(data:SddRights) {
        val dialog = SendMoreTipDialog(context,data)
        dialog.show()
    }

    private fun initHeight() {
        for (key in heights.keys) {
            heights[key] = key.height
        }
        mHeight = height
        mShowHeight = 0
        val lp = layoutParams
        heights.values.forEachIndexed { index, i ->
            if (index <= showViewSize - 1) {
                mShowHeight += i
            }
        }
        if (!isExpand) {
            lp.height = mShowHeight
            requestLayout()
        }
    }

    fun expand(expand: Boolean) {
        isExpand = if (expand) {
            //展开
            anim(mShowHeight, mHeight)
            true
        } else {
            //缩小
            anim(mHeight, mShowHeight)
            false
        }
    }

    private fun anim(startHeight: Int, endHeight: Int) {
        val animation = ExpandAnimation(this, startHeight, endHeight)
        animation.fillAfter = true
        animation.duration = 400
        animation.setAnimationListener(object : Animation.AnimationListener {
            override fun onAnimationRepeat(animation: Animation?) {

            }

            override fun onAnimationEnd(animation: Animation?) {
                clearAnimation()
            }

            override fun onAnimationStart(animation: Animation?) {

            }

        })
        startAnimation(animation)
    }
}